package rigo.ricard.pris.rs.ac.uns.dmi.jsf.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

@FacesConverter(value="doubleConverter")
public class DoubleConverter implements Converter{

	private static final Log log = LogFactoryUtil.getLog(DoubleConverter.class);
	
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if (arg2.trim().equals("")) {
            return null;
		}
		try {
			double number = Double.parseDouble(arg2);
			return number;
		} catch (Exception e) {
			log.error("error occured during converting string to double");
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		if (arg2 == null || arg2.equals("")) {
            return "";
		} else {
            //format double to string you want
            return ((Double)arg2) + "";
        }
	}
	
	

}
